<?php
namespace EasyPhpWeb;

class Response{
    //请求状态
    private $code = '200';

    public function write($data, $stream){
        $response = "HTTP/1.1 {$this->code} OK\r\n";
        $response .= "Content-Type: text/html;charset=UTF-8\r\n";
        // $response .= "Server: MyServer1\r\n";
        $response .= "Content-length: " . strlen($data) . "\r\n\r\n";
        $response .= $data;
        fwrite($stream, $response);
    }

    /**
     * 直接返回文件，用于处理静态文件
     */
    public function file($path, $stream){
        $data = file_get_contents($path);
        $type = mime_content_type($path);

        $response = "HTTP/1.1 {$this->code} OK\r\n";
        $response .= "Content-Type: {$type};charset=UTF-8\r\n";
        // $response .= "Server: MyServer1\r\n";
        $response .= "Content-length: " . strlen($data) . "\r\n\r\n";
        $response .= $data;
        fwrite($stream, $response);
    }
}